package com.mdd.front.controller;

import com.alipay.api.AlipayApiException;
import com.mdd.common.aop.NotLogin;
import com.mdd.common.aop.NotPower;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.exception.OperateException;
import com.mdd.common.validator.annotation.IDMust;
import com.mdd.front.service.IUserOrderService;
import com.mdd.front.validate.common.IdValidate;
import com.mdd.front.validate.order.OrderClueCreateValidate;
import com.mdd.front.validate.order.OrderCreateValidate;
import com.mdd.front.vo.order.OrderInfoDetailVo;
import com.mdd.front.vo.order.OrderInfoListedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * 订单
 * <p>
 * {@code @Description}
 * {@code @Author} 幻速
 * {@code @Date} 2023/11/1 17:44
 **/
@RestController
@RequestMapping("/api/order")
@Api(tags = "我的订单")
public class OrderController {
    @Resource
    IUserOrderService iOrderService;
    //订单列表
    @GetMapping("/list")
    public AjaxResult<PageResult<OrderInfoListedVo>> list(@Validated PageValidate pageValidate, @RequestParam(name = "status", required = false) Integer status){
        return AjaxResult.success(iOrderService.list(status,pageValidate));
    }


    //订单详情
    @GetMapping("/detail")
    public AjaxResult<OrderInfoDetailVo> detail(@Validated @IDMust() @RequestParam("orderId") Long orderId){
        return AjaxResult.success(iOrderService.detail(orderId));
    }
    //添加订单接口
    //取消订单
    @GetMapping("/cancel")
    public AjaxResult<Object> cancel(@Validated @IDMust() @RequestParam("orderId") Long orderId){
        try {
            iOrderService.cancel(orderId);
        } catch (AlipayApiException e) {
            throw new OperateException(e.getErrMsg());
        }
        return AjaxResult.success();
    }
    @NotPower
    @PostMapping("/addPreOrder")
    @ApiOperation(value="预订单新增")
    public AjaxResult<Object> addPreOrder(@Validated @RequestBody OrderCreateValidate createValidate) {
        Long orderId =iOrderService.addPreOrder(createValidate);
        return AjaxResult.success(orderId);
    }
    @PostMapping("/finishPreOrder")
    @ApiOperation(value="完成预订单新增")
    public AjaxResult<Object> finishPreOrder(@Validated @RequestBody IdValidate idValidate) {
        iOrderService.finishPreOrder(idValidate.getId());
        return AjaxResult.success();
    }
    //申请售后
    @PostMapping("/refund")
    public AjaxResult<Object> refund(){
        return null;
    }


    @NotLogin
    @PostMapping("/addOrderClue")
    @ApiOperation(value="线索新增")
    public AjaxResult<Object> addOrderClue(@Validated @RequestBody OrderClueCreateValidate createValidate) {
        return AjaxResult.success(iOrderService.addOrderClue(createValidate));
    }
    @NotLogin
    @PostMapping("/getOrderClueById")
    @ApiOperation(value="线索详情")
    public AjaxResult<Object> getOrderClueById(@Validated @RequestBody IdValidate idValidate) {
        return AjaxResult.success(iOrderService.getOrderClueById(idValidate.getId()));
    }
}
